package com.example.a4_2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //保存数据
        Button saveButton = (Button) findViewById(R.id.btn1);
        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
                editor.putString("name", "Tom");
                editor.putInt("age", 23);
                editor.putBoolean("isMarried", false);
                editor.apply();
            }
        });

        //读取数据
        Button getButton = (Button) findViewById(R.id.btn2);
        getButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences getData = getSharedPreferences("data", MODE_PRIVATE);
                String name = getData.getString("name", "");
                int age = getData.getInt("age", 0);
                Boolean isMarried = getData.getBoolean("isMarried", false);
                Log.d("SaveForPre", "name is " + name);
                Log.d("SaveForPre", "age is " + age);
                Log.d("SaveForPre", "isMarried is " + isMarried);
            }
        });
    }
}